# 使用华为云镜像，包含CUDA 11.8 + cuDNN8 + Ubuntu 22.04
FROM swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04

# 设置基础环境变量
ENV DEBIAN_FRONTEND=noninteractive

# 设置工作目录
WORKDIR /app

# 配置清华源
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse" > /etc/apt/sources.list && \
    echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse" >> /etc/apt/sources.list

# 安装基础依赖和OpenGL库
RUN apt-get update && apt-get install -y \
    ca-certificates \
    curl \
    wget \
    bzip2 \
    openssh-server \
    sudo \
    libgl1-mesa-glx \
    libglib2.0-0 \
    libsm6 \
    libxext6 \
    libxrender-dev \
    libgomp1 \
    && rm -rf /var/lib/apt/lists/*

# 下载并安装Miniconda
RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O /tmp/miniconda.sh && \
    bash /tmp/miniconda.sh -b -p /opt/miniconda3 && \
    rm /tmp/miniconda.sh

# 设置Miniconda和CUDA环境变量
ENV PATH="/opt/miniconda3/bin:/usr/local/cuda-11.8/bin:${PATH}" \
    CUDA_HOME="/usr/local/cuda-11.8" \
    LD_LIBRARY_PATH="/usr/local/cuda-11.8/lib64:${LD_LIBRARY_PATH}" \
    CONDA_ALWAYS_YES=true \
    CONDA_AUTO_ACTIVATE_BASE=false

# 初始化conda
RUN /opt/miniconda3/bin/conda init bash

# 创建ai虚拟环境
RUN /opt/miniconda3/bin/conda create --name ai python=3.10 -c conda-forge --override-channels -y

# 先安装基础依赖包 - 使用NumPy 1.x版本避免兼容性问题
RUN /opt/miniconda3/bin/conda run -n ai conda install "numpy<2.0" libpng pillow -c conda-forge --override-channels -y

# 安装PyTorch (CUDA 11.8版本) - 使用pip安装避免服务条款问题
# 增加网络重试和超时配置，解决下载失败问题
RUN /opt/miniconda3/bin/conda run -n ai pip install --default-timeout=1000 --resume-retries=50 --retries=10 torch==2.3.1 torchvision==0.18.1 torchaudio==2.3.1 --index-url https://download.pytorch.org/whl/cu118 --root-user-action=ignore --no-warn-script-location

# 先安装OpenCV无头版本 - 避免OpenGL库依赖问题
RUN /opt/miniconda3/bin/conda run -n ai pip install opencv-python-headless==4.6.0.66 --root-user-action=ignore --no-warn-script-location

# 安装MMCV - 不安装依赖，避免自动安装高版本OpenCV
RUN /opt/miniconda3/bin/conda run -n ai pip install mmcv==2.1.0 -f https://download.openmmlab.com/mmcv/dist/cu118/torch2.3/index.html --no-deps --root-user-action=ignore --no-warn-script-location

# 安装MMCV的其他必要依赖
RUN /opt/miniconda3/bin/conda run -n ai pip install addict pyyaml yapf --root-user-action=ignore --no-warn-script-location

# 安装其他MM系列包
RUN /opt/miniconda3/bin/conda run -n ai pip install -U openmim -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN /opt/miniconda3/bin/conda run -n ai mim install mmengine==0.10.3 -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN /opt/miniconda3/bin/conda run -n ai mim install mmpretrain==1.2.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN /opt/miniconda3/bin/conda run -n ai mim install mmsegmentation==1.2.2 -i https://pypi.tuna.tsinghua.edu.cn/simple
# 复制requirements.txt文件
COPY requirements.txt /app/requirements.txt

# 使用requirements.txt安装其他依赖包
RUN /opt/miniconda3/bin/conda run -n ai pip install -r /app/requirements.txt --root-user-action=ignore --no-warn-script-location -i https://pypi.tuna.tsinghua.edu.cn/simple




# 配置SSH服务
RUN mkdir /var/run/sshd && \
    echo 'root:123' | chpasswd && \
    sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && \
    sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config && \
    sed -i 's/#Port 22/Port 22/' /etc/ssh/sshd_config && \
    chmod 600 /etc/ssh/sshd_config && \
    ssh-keygen -A

# 配置SSH环境变量
RUN echo 'export CUDA_HOME=/usr/local/cuda-11.8' >> /root/.bashrc && \
    echo 'export PATH=/usr/local/cuda-11.8/bin:$PATH' >> /root/.bashrc && \
    echo 'export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64:$LD_LIBRARY_PATH' >> /root/.bashrc && \
    echo 'export PATH=/opt/miniconda3/bin:$PATH' >> /root/.bashrc

# 暴露SSH端口
EXPOSE 22

# 创建启动脚本
RUN echo '#!/bin/bash' > /app/start.sh && \
    echo 'service ssh start' >> /app/start.sh && \
    echo 'ps aux | grep sshd' >> /app/start.sh && \
    echo 'tail -f /dev/null' >> /app/start.sh && \
    chmod +x /app/start.sh

# 设置启动命令 - 启动SSH服务和Gunicorn FastAPI服务器
CMD ["bash", "-c", "service ssh start && /opt/miniconda3/bin/conda run -n ai gunicorn pyrun.http.server:app --bind 0.0.0.0:7012 --workers 4 --worker-class uvicorn.workers.UvicornWorker --worker-connections 1000 --max-requests 1000 --max-requests-jitter 100 --timeout 300 --keep-alive 2 --access-logfile - --error-logfile - --log-level info"]